package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.FeeValue;
import com.atguigu.lease.web.admin.mapper.FeeKeyMapper;
import com.atguigu.lease.web.admin.service.FeeKeyService;
import com.atguigu.lease.web.admin.service.FeeValueService;
import com.atguigu.lease.web.admin.vo.FeeKeyVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.lease.model.entity.FeeKey;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 1
* @description 针对表【fee_key(杂项费用名称表)】的数据库操作Service实现
* @createDate 2024-08-28 11:00:26
*/
@Service
public class FeeKeyServiceImpl extends ServiceImpl<FeeKeyMapper, FeeKey>
    implements FeeKeyService {
    @Resource
    FeeValueService feeValueService;
    /*
        feeKey查询了所有的未删除的key：fee_key
        feeValue 最终组合起来也是fee_value表所有的未删除数据

            只需要查询两次数据，在业务代码中组装数据即可
     */
    @Override
    public List<FeeKeyVo> listFeeKeyVos() {
        //表数据量不大 两张表连接字段没有索引时推荐使用
        List<FeeKey> feeKeys = this.list();
        List<FeeValue> feeValues = feeValueService.list();

        return feeKeys.stream().map(feeKey -> {
            //创建新的vo对象
            FeeKeyVo feeKeyVo = new FeeKeyVo();
            //将feekey属性值复制给vo对象
            BeanUtils.copyProperties(feeKey , feeKeyVo);
            //从feevalue集合中挑出 feeKeyVo的值：   feeValue的feeKeyId 如果等于feeKey的id 那么他就是这个key的值
            List<FeeValue> feeValueList = feeValues.stream()
                    .filter(feeValue -> feeValue.getFeeKeyId().equals(feeKey.getId()))
                    .collect(Collectors.toList());
            feeKeyVo.setFeeValueList(feeValueList);
            return feeKeyVo;
        }).collect(Collectors.toList());


    }
//    @Override
//    public List<FeeKeyVo> listFeeKeyVos() {
//        List<FeeKey> feeKeys = this.list();
//        //遍历集合 将每个对象转为指定类型的对象
//        return feeKeys.stream().map(feeKey -> {
//            FeeKeyVo feeKeyVo = new FeeKeyVo();
//            // BeanUtils 可以将数据源对象的相同类型相同名称的属性值设置给目标对象
//            BeanUtils.copyProperties(feeKey , feeKeyVo);
//            //需要给feeKeyVo对象查询feeValueList集合
//            List<FeeValue> feeValues = feeValueService.list(Wrappers.lambdaQuery(FeeValue.class)
//                    .eq(FeeValue::getFeeKeyId, feeKey.getId()));
//            feeKeyVo.setFeeValueList(feeValues);
//            //返回转换后的vo对象
//            return feeKeyVo;
//        }).collect(Collectors.toList());
//    }
}




